home *** CD-ROM | disk | FTP | other *** search
/ Chip 2006 June / CHIP 2006-06.2.iso / program / freeware / Democracy-0.8.2.exe / xulrunner / python / templatehelper.py < prev    next >
Encoding:
Python Source  |  2006-04-10  |  2.3 KB  |  91 lines

  1. # templatehelper.py Copyright (c) 2005,2006 Participatory Culture Foundation
  2. # Contains code shared by template_compiler and template
  3.  
  4. # Distutils needs this in a .py file, so it knows they're required
  5. # by the entension module. This place seems as good as any.
  6. import cPickle #for database.pyx
  7.  
  8. import gettext
  9. import shutil
  10. import re
  11. import types
  12. import traceback
  13. import random
  14. from xhtmltools import toUTF8Bytes, urlencode
  15.  
  16. HTMLPattern = re.compile("^.*<body.*?>(.*)</body\s*>", re.S)
  17. attrPattern = re.compile("^(.*?)@@@(.*?)@@@(.*)$")
  18. resourcePattern = re.compile("^resource:(.*)$")
  19. rawAttrPattern = re.compile("^(.*)\*\*\*(.*?)\*\*\*(.*)$")
  20. evalCache = {}
  21.  
  22. # Constants for the compiler and runtime function tables
  23. # Eventually, we should eliminate these and use the actual functions
  24. textFunc = 0
  25. textHideFunc = 1
  26. attrFunc = 2
  27. addIDFunc = 3
  28. evalEscapeFunc = 4
  29. evalFunc = 5
  30. includeHideFunc = 6
  31. hideIfEmptyFunc = 7
  32. rawAttrFunc = 8
  33. hideSectionFunc = 9
  34. quoteAndFillFunc = 10
  35.  
  36. def quoteattr(orig):
  37.     return orig.replace('"','"')
  38.  
  39. def escape(orig):
  40.     return unicode(orig).replace('&','&').replace('<','<').replace('>','>')
  41.  
  42. # 'key' is a key name in the template language. Resolve it relative to 'data.'
  43. # For example, 'this feed name' might become data['this'].feed.name().
  44. def evalKey(keyString, indata, originalKey = None, cache = False):
  45.     global evalCache
  46.  
  47.     data = indata
  48.  
  49.     if cache and evalCache.has_key(keyString):
  50.         return evalCache[keyString]
  51.  
  52.     # Save the original expression for use in error messages
  53.     if originalKey is None:
  54.         originalKey = keyString
  55.  
  56.     keys = keyString.split()
  57.  
  58.     for key in keys:
  59.         try:
  60.             data = data[key]
  61.         except:
  62.             try:
  63.                 data = getattr(data, key)
  64.             except:
  65.                 return 'Bad Key'
  66.             try:
  67.                 data = data()
  68.             except:
  69.                 pass
  70.     if cache:
  71.         evalCache[keyString] = data
  72.     return data
  73.  
  74. # Clears cache for evalKey
  75. def clearEvalCache():
  76.     global evalCache
  77.     evalCache = {}
  78.  
  79. def toUni(orig):
  80.     if type(orig) == types.IntType:
  81.         return "%d" % orig
  82.     else:
  83.         orig = toUTF8Bytes(orig)
  84.         return unicode(orig,'utf-8')
  85.  
  86.  
  87. # Generate an arbitrary string to use as an ID attribute.
  88. def generateId():
  89.     return "tmplcomp%08d" % random.randint(0,99999999)
  90.  
  91.